package com.fivefu.core.hik.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fivefu.core.hik.entity.DbHikRegionsDevice;
import com.fivefu.core.hik.mapper.DbHikRegionsDeviceMapper;
import com.fivefu.core.hik.services.DbHikRegionsDeviceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 海康区域设备关联表 服务实现类
 * </p>
 *
 * @author 
 * @since 2022-07-12 09:26:17
 */
@Service
public class DbHikRegionsDeviceServiceImpl extends ServiceImpl<DbHikRegionsDeviceMapper, DbHikRegionsDevice> implements DbHikRegionsDeviceService {

    /**
     * 删除其他区域下关联编码设备信息
     * @param notContainRegionIndexCodes   排除区域唯一标识
     */
    @Override
    public void deleteNotContainRegionsDeviceByRegionIndexCodes(List<String> notContainRegionIndexCodes) {
        QueryWrapper<DbHikRegionsDevice> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        if (!notContainRegionIndexCodes.isEmpty()) {
            wrapper.in("region_index_code",notContainRegionIndexCodes);
        }

        DbHikRegionsDevice regionsDevice = new DbHikRegionsDevice();
        regionsDevice.setIsDelete(1);
        regionsDevice.setUpdatedTime(LocalDateTime.now());
        //删除编码设备关联
        baseMapper.update(regionsDevice,wrapper);
    }

    /**
     * 查询区域编码设备Map集合
     * @return
     */
    @Override
    public Map<String, Map<String, Long>> queryRegionDeviceRegionMap() {
        QueryWrapper<DbHikRegionsDevice> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        wrapper.select("id","region_index_code","index_code");

        List<DbHikRegionsDevice> regionsDeviceList = baseMapper.selectList(wrapper);
        //stream流根据所属区域编码分组，所属区域编码为map的键，设备唯一编码和id为值
        Map<String,Map<String,Long>> strMap = regionsDeviceList.stream().collect(Collectors.groupingBy(DbHikRegionsDevice:: getRegionIndexCode, Collectors.toMap(DbHikRegionsDevice :: getIndexCode, DbHikRegionsDevice::getId)));
        return strMap;
    }

    /**
     * 删除区域下编码设备
     * @param regionIndexCode  区域唯一标识
     * @param regionDeviceIds  设备唯一编码集合
     */
    @Override
    public void deleteRegionDevice(String regionIndexCode, List<Long> regionDeviceIds) {
        QueryWrapper<DbHikRegionsDevice> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        wrapper.eq("region_index_code",regionIndexCode);
        wrapper.in("id",regionDeviceIds);
//        wrapper.in("index_code",regionDeviceIds);

        DbHikRegionsDevice device = new DbHikRegionsDevice();
        device.setIsDelete(1);
        device.setUpdatedTime(LocalDateTime.now());
        //删除编码设备关联
        baseMapper.update(device,wrapper);
    }

    /**
     * 批量增加区域编码设备信息
     * @param regionIndexCode   区域唯一标识
     * @param deviceIndexCodeList    编码设备唯一编码集合
     */
    @Override
    public void insertRegionDevices(String regionIndexCode, List<String> deviceIndexCodeList) {
        LocalDateTime time = LocalDateTime.now();
        List<DbHikRegionsDevice> deviceList = new ArrayList<>();
        for (String deviceIndexCode : deviceIndexCodeList) {
            DbHikRegionsDevice regionsDevice = new DbHikRegionsDevice();
            regionsDevice.setRegionIndexCode(regionIndexCode);
            regionsDevice.setIndexCode(deviceIndexCode);
            regionsDevice.setCreatedTime(time);
            regionsDevice.setIsDelete(0);
            deviceList.add(regionsDevice);
        }
        saveBatch(deviceList);
    }

    /**
     * 根据编码设备唯一编码查询
     * @param deviceCode
     * @return
     */
    @Override
    public DbHikRegionsDevice queryRegionByDeviceCode(String deviceCode) {
        QueryWrapper<DbHikRegionsDevice> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        wrapper.eq("index_code",deviceCode);
        DbHikRegionsDevice regionsDevice = baseMapper.selectOne(wrapper);
        return regionsDevice;
    }

    /**
     * 查询所有区域编码设备关联关系
     * @return
     */
    @Override
    public List<DbHikRegionsDevice> queryRegionDevice() {
        QueryWrapper<DbHikRegionsDevice> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        List<DbHikRegionsDevice> regionsDeviceList = baseMapper.selectList(wrapper);
        return regionsDeviceList;
    }
}
